package com.whfc.fvs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dao.FvsSnapshotMapper;
import com.whfc.fvs.dto.FvsLogDTO;
import com.whfc.fvs.dto.FvsSnapshotDTO;
import com.whfc.fvs.entity.FvsSnapshot;
import com.whfc.fvs.service.FvsSnapshotService;
import com.whfc.fvs.service.manager.FvsSnapshotMgr;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-02-24 14:15
 */
@DubboService(interfaceClass = FvsSnapshotService.class, version = "1.0.0", timeout = 10 * 1000)
public class FvsSnapshotServiceImpl implements FvsSnapshotService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FvsSnapshotMapper fvsSnapshotMapper;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @Autowired
    private FvsSnapshotMgr snapshotMgr;

    @Override
    public PageData<FvsSnapshotDTO> list(Integer deptId, Integer pageNum, Integer pageSize, Date startDate, Date endDate, String keyword) throws BizException {
        logger.info("查询项目视频抓拍记录");
        if (startDate != null) {
            startDate = DateUtil.getDateBegin(startDate);
        }
        if (endDate != null) {
            endDate = DateUtil.getDateEnd(endDate);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<FvsSnapshotDTO> list = fvsSnapshotMapper.selectSnapshotList(deptId, startDate, endDate, keyword);
        PageInfo<FvsSnapshotDTO> pageInfo = PageInfo.of(list);
        return PageUtil.pageData(pageInfo);
    }

    @Override
    public void createSnapshot(Integer deptId) throws BizException {
        if (deptId == null) {
            return;
        }
        snapshotMgr.createSnapshot(deptId);
    }

    @Override
    public FvsLogDTO fvsLog(Integer deptId, Date date) {
        Integer captureNum = fvsSnapshotMapper.selectSnapshotNum(deptId, date);
        Integer deviceNum = fvsDeviceMapper.selectFvsDeviceNum(deptId, date);
        FvsLogDTO fvsLogDTO = new FvsLogDTO();
        fvsLogDTO.setCaptureNum(captureNum);
        fvsLogDTO.setDeviceNum(deviceNum);
        return fvsLogDTO;
    }

    @Override
    public void batchInsert(List<FvsSnapshotDTO> snapshotListDTOs) {
        List<FvsSnapshot> snapshotList = new ArrayList<>();
        FvsSnapshot fvsSnapshot;
        for (FvsSnapshotDTO snapshotListDTO : snapshotListDTOs) {
            fvsSnapshot = new FvsSnapshot();
            BeanUtils.copyProperties(snapshotListDTO,fvsSnapshot);
            snapshotList.add(fvsSnapshot);
        }
        fvsSnapshotMapper.batchInsert(snapshotList);
    }
}
